<!--设备参数设置 model 页-->
function updatePram(eid, pramName, unit) {
    bootbox.prompt({
        size: 'small',
        title: "参数设置-" + pramName + " 范围:(-10 ~ 10),步进为:0.05 (单位:" + unit + ")",
        callback: function (result) {
            transAndSentPower(eid,result);
        }
    });
}

function transAndSentPower(eid,result) {
    if (result != "" && result !== null && ! isNaN(result)) {
        var tempVal = parseInt(mul(add(result,10),100));
        // var tempVal = parseInt((parseFloat(result)+10)*100);//js 浮数bug
        var requireVal = tempVal / 5;
        if(tempVal % 5 == 0 && (tempVal >= 0 && tempVal <= 2000)){
            onAjaxSwitch(ctx+"/biz/basic/sendcmd?device="+eid+"&cmd=power&value="+requireVal);
            return tempVal;
        }
    }
    if(result != null){
        Messenger().post("提交失败:参数不合法!");
    }
}

function updatePram2(eid, pramName, unit) {
    bootbox.prompt({
        size: 'small',
        title: "参数设置-" + pramName +"(单位:" + unit + ")",
        callback: function (result) {
            onAjaxSwitch(ctx+"/biz/basic/sendcmd?device="+eid+"&cmd=freq&value="+result);
        }
    });
}

function add(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length;
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length;
    } catch (f) {
        d = 0;
    }
    return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e;
}

function mul(a, b) {
    var c = 0,
        d = a.toString(),
        e = b.toString();
    try {
        c += d.split(".")[1].length;
    } catch (f) {}
    try {
        c += e.split(".")[1].length;
    } catch (f) {}
    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}

//执行交接班操作
function showModel() {
    var currenctUserId = $("#currenctUserId").val();
    var handoverUserId = $("#handoverUserId").val();

    //TODO 检查当前用与是否是值班用户
    var checkUser = true;
    if (currenctUserId != undefined && handoverUserId != undefined && currenctUserId != handoverUserId) {
        bootbox.confirm({
            title: '提示',
            message: "当前登录用户非值班用户，是否进行顶班操作?",
            callback: function (result) {
                if (result) {
                    executeHandover(true);
                    return true;
                }
                // return true;
            }
        });
    } else {
        executeHandover(false);
    }
}

function executeHandover(status) {
    var step = 1;
    var radioValue;
    if (status) {
        radioValue = "option3"; //表示顶班操作
    }

    //判断当前时间是否是交班时间
    $.ajax({
        url: ctx + "/biz/handoverInfo/checkTime",
        success: function (response) {
            if ("true" == response || status) { //在交接班时间段内  或者执行的顶班操作
                bootbox.confirm({
                    title: "交接班",
                    message: $("#dutyActionTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g, ""),
                    callback: function (result) {
                        if (result && checkForm()) {
                            submitHandover(radioValue);
                            //提交交班信息
                            // var returnValue = submitHandover(radioValue);
                            // alert("returnValue is :" + returnValue);
                            // return returnValue;
                        }
                        return false;
                    }
                });
                renderSelects();
            } else {  //在非交接班时间段内
                bootbox.confirm({
                    title: "交接班",
                    message: $("#confirmTplExp").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g, ""),
                    callback: function (result) {
                        if (step == 1 && result) {
                            //判断是否显示第异常交接班信息
                            radioValue = $("input[name='inlineRadioOptions']:checked").val();
                            if (radioValue == undefined || radioValue == '') {
                                step = 1;
                                $(".exPagePre").removeClass("hide");
                            } else {
                                $("#innerModel").html($("#dutyActionTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g, ""));
                                renderSelects();
                                step++;
                            }
                            return false;
                        } else if (step == 2 && result) {
                            if (result && checkForm()) {
                                submitHandover(radioValue);
                                return false;
                            } else {
                                return false;
                            }
                        }
                    }
                });
            }
        }
    })
}

//提交申请信息
function submitHandover(radioValue) {
    //提交交班信息
    $.ajax({
        url: ctx + "/biz/handoverInfo/save",
        async: false,
        data: {
            'provider.id': $("#handoverUserId").val(),
            'provider': $("#provider").text(),
            'successor.id': $("#successor").val(),
            'successor': $("#successor option:selected").text(),
            'note': $("#note").val(),
            'exceptionType': radioValue,
            'password': $("#password").val()
        },
        success: function (response) {
            var resultJson = eval("(" + response + ")");
            if (resultJson.success) {
                Messenger().post(resultJson.message);
                $("#handoverHide").html(resultJson.message);
                //刷新主页
                location.reload();
                return true;
            } else {
                $("#handoverHide").html(resultJson.message);
                return false;
            }
        }
    });
}

//检查输入项
function checkForm() {
    var password = $("#password").val();
    var note = $("#note").val();
    if (password == undefined || password == "") {
        $("#handoverHide").html("请输入密码！");
        return false;
    }

    if (note == undefined || note == "") {
        $("#handoverHide").html("请输入遗留问题！");
        return false;
    }
    return true;
}

// 当天交接班情况
function showDayLogTpl(title, tplId) {
    bootbox.alert({
        title: title,
        message: $("#" + tplId).html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g, ""),
        callback: function (result) {
            if (result) {
                Messenger().post("提交成功!");
            }
        }
    });
}

// setInterval(testtime,1000);
// function testtime(){
//     var now = new Date();
//     var hours=now.getHours();
//     var minutes=now.getMinutes();
//     var sec = now.getSeconds();
//     console.info("hours",hours);
//     console.info("minutes",sec);
//     console.info("minutes", sec > 30 );
// };



